package com.zcxy360.sandtable.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcxy360.sandtable.domain.Plan;
import com.zcxy360.sandtable.domain.PlanExample;
import com.zcxy360.sandtable.domain.PlanExample.Criteria;
import com.zcxy360.sandtable.domain.PlanMapper;
import com.zcxy360.sandtable.domain.User;
import com.zcxy360.sandtable.domain.VplanMapper;
import com.zcxy360.sandtable.util.PdfUtil;
import com.zcxy360.sandtable.util.ShiroUtil;
import com.zcxy360.sandtable.util.StringUtil;
import com.zcxy360.sandtable.util.UploadUtil;

@Transactional
@Service
public class PlanService {
	
	@Value("${res.server}")
	private String resServer;

	@Autowired
	private PlanMapper planMapper;
	@Autowired
	private VplanMapper vplanMapper;

	public Object getByPlanId(Integer planId,Integer deep) {
		Plan plan = null;
		switch (deep) {
		case 0:
			plan = planMapper.selectByPrimaryKey(planId);
			break;
		case 1:
			plan = planMapper.selectByPrimaryKeyWithExtra(planId);
			break;
		default:
			break;
		}
		return plan;
	}

	public Object list(Plan plan, String key, String orderByClause,Integer sign, Integer pageNum, Integer pageSize, Integer navigatePages) {
		User loginUser = ShiroUtil.getUser();
		String loginRole = loginUser.getRole();
		if(!loginRole.equals("admin")) {
			plan.setOrgId(loginUser.getOrgId());
		}
		if(sign != null) {
			//只看自己
			if(sign == 1 && !loginRole.equals("admin")) {
				plan.setUserId(loginUser.getUserId());
			}
			//看非自己的其他老师资源
			if(sign == 2) {
				plan.setUserId(loginUser.getUserId());
			}
			//看内建资源
			if(sign == 3) {
				plan.setUserId(1);
				plan.setOrgId(null);
			}
			//看本机构自己的所有资源及其它老师共享资源及内建资源
			if(sign == 4) {
				plan.setUserId(loginUser.getUserId());
			}
			//看本机构所有资源及内建资源
			if(sign == 5) {
			}
		}
		PageHelper.startPage(pageNum, pageSize);
		List<Plan> plans = planMapper.listWithExtra(plan,StringUtil.wrapper(key),orderByClause,sign);
		return new PageInfo<>(plans, navigatePages);
	}
	
	public Plan post(MultipartFile file,Plan plan) {
		User loginUser = ShiroUtil.getUser();
		plan.setUserId(loginUser.getUserId());
		plan.setOrgId(loginUser.getOrgId());
		Integer type = plan.getType();
		if(type != null && type == 2) {
			if(file == null) {
				throw new RuntimeException("请上传文件");
			}
			resolvePlan(file,plan);
		}else {
			int wordCount = StringUtil.count(plan.getContent());
			plan.setWordCount(wordCount);
			plan.setRoughTime((int)(Math.ceil(wordCount/100d)*5));
		}
		planMapper.insertSelective(plan);
		vplanMapper.insertByPlanId(plan.getPlanId());
		return plan;
	}


	public boolean update(MultipartFile file,Plan plan) {
		User loginUser = ShiroUtil.getUser();
		String loginRole = loginUser.getRole();
		if(!loginRole.equals("admin")) {
			Plan p = planMapper.selectByPrimaryKey(plan.getPlanId());
			if(loginRole.equals("teacher")) {
				if(!p.getUserId().equals(loginUser.getUserId())) {
					throw new RuntimeException("老师只能修改本人的资源");
				}
			}
			if(loginRole.equals("manager")) {
				if(!p.getOrgId().equals(loginUser.getOrgId())) {
					throw new RuntimeException("机构管理员只能修改本机构的资源");
				}
			}
		}
		Integer type = plan.getType();
		if(type != null) {
			if(type == 1) {
				if(StringUtil.isNotEmpty(plan.getContent())) {
					int wordCount = StringUtil.count(plan.getContent());
					plan.setWordCount(wordCount);
					plan.setRoughTime((int)(Math.ceil(wordCount/100d)*5));
				}
			}
			if(type == 2) {
				if(file != null) {
					resolvePlan(file,plan);
				}
			}
		}
		planMapper.updateByPrimaryKeySelective(plan);
		vplanMapper.insertByPlanId(plan.getPlanId());
		return true;
	}

	public boolean delete(int[] planIds) {
		User loginUser = ShiroUtil.getUser();
		String loginRole = loginUser.getRole();
		Plan p;
		if(!loginRole.equals("admin")) {
			for(int planId:planIds) {
				p = planMapper.selectByPrimaryKey(planId);
				if(loginRole.equals("teacher")) {
					if(!p.getUserId().equals(loginUser.getUserId())) {
						throw new RuntimeException("老师只能删除本人的资源");
					}
				}
				if(loginRole.equals("manager")) {
					if(!p.getOrgId().equals(loginUser.getOrgId())) {
						throw new RuntimeException("机构管理员只能删除本机构的资源");
					}
				}
			}
		}
		planMapper.deleteBatch(planIds);
		return true;
	}

	public boolean checkName(String name) {
		User loginUser = ShiroUtil.getUser();
		PlanExample ex = new PlanExample();
		Criteria or = ex.or();
		or.andNameEqualTo(name);
		if(loginUser.getRole().equals("admin")) {
			or.andOrgIdIsNull();
		}else {
			or.andOrgIdEqualTo(loginUser.getOrgId());
		}
		return planMapper.countByExample(ex) == 0;
	}
	
	private void resolvePlan(MultipartFile file,Plan plan) {
		String path = UploadUtil.uploadPdf(file);
		String content = PdfUtil.readPdf(path);
		plan.setPath(path.replace("E:/res", resServer));
		int wordCount = StringUtil.count(content);
		plan.setWordCount(wordCount);
		plan.setRoughTime((int)(Math.ceil(wordCount/100d)*5));
	}
	
}

